package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Client;
import DataStorageClasses.Employee;
import Database.DatabaseConnector;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
	
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();

		String busName = (String) session.getAttribute("username");
		long empID = Long.parseLong(request.getParameter("employee"));

		Employee emp = dbConnect.getEmployee(empID, busName);

		JSONObject result = new JSONObject();
		
		try {
			result.put("id", emp.getEmployeeID());
			result.put("employeeFirstName", emp.getFirstName());
			result.put("employeeLastName", emp.getLastName());
			
			result.put("employer", emp.getBusinessName());
			result.put("telephone", emp.getHomeTel());
			result.put("cellPhone", emp.getCell());
			result.put("dateHired", emp.getDateHired());
			result.put("dateFired", emp.getDateFired());
			result.put("wage", emp.getWage());
			result.put("SIN", emp.getSIN());
			result.put("address", emp.getStreet());
			result.put("city", emp.getCity());
			result.put("province", emp.getProvince());
			result.put("postalCode",emp.getPostalCode());
			result.put("employeeID",emp.getEmployeeID());
			
			
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
		res.getWriter().print(result);
		res.setStatus(HttpURLConnection.HTTP_ACCEPTED);

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
	
		DatabaseConnector dbConnect = new DatabaseConnector();

		long empID = Long.parseLong(request.getParameter("employee"));
		
		dbConnect.removeEmployee(empID);
	
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
	
}